package com.trdcore.zyzkt.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.trdcore.zyzkt.model.vod.Course;
import com.trdcore.zyzkt.resp.ResultData;
import com.trdcore.zyzkt.service.CourseService;
import com.trdcore.zyzkt.vo.vod.CourseFormVo;
import com.trdcore.zyzkt.vo.vod.CoursePublishVo;
import com.trdcore.zyzkt.vo.vod.CourseQueryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Luffy
 * @since 2024-03-18
 */
@Tag(name = "课程管理接口")
@RestController
@RequestMapping(value="/admin/vod/course")

public class CourseController {

    @Autowired
    private CourseService courseService;

    @Operation(summary = "获取分页列表",description = "获取分页列表")
    @GetMapping("/{page}/{limit}")
    public ResultData index(
            @Parameter(name = "page",description="当前页码",required = true)
            @PathVariable("page") Long page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable("limit") Long limit,
            @Parameter(name = "courseVo", description = "查询对象", required = false)
            CourseQueryVo courseQueryVo) {
        Page<Course> pageParam = new Page<>(page, limit);
        Map<String,Object> map = courseService.findPage(pageParam, courseQueryVo);
        return ResultData.success(map);
    }

    //添加课程基本信息
    @Operation(summary = "添加课程基本信息",description = "添加课程基本信息")
    @PostMapping("/save")
    public ResultData save(@RequestBody CourseFormVo courseFormVo){
        Long courseId=courseService.saveCourse(courseFormVo);
        return ResultData.success(courseId);
    }

    //根据ID获取课程信息
    @Operation(summary = "根据ID获取课程信息",description = "根据ID获取课程信息")
    @GetMapping("/get/{id}")
    public ResultData get(@PathVariable("id") Long id){
        CourseFormVo courseFormVo=courseService.getCourseInfoById(id);
        return ResultData.success(courseFormVo);
    }

    //修改课程信息
    @Operation(summary = "修改课程信息",description = "修改课程信息")
    @PostMapping("/update")
    public ResultData updateCourse(@RequestBody CourseFormVo courseFormVo){
        courseService.updateCourseId(courseFormVo);
        return ResultData.success(courseFormVo.getId());
    }

    @Operation(summary="根据id获取课程发布信息",description="根据id获取课程发布信息")
    @GetMapping("/getCoursePublishVo/{id}")
    public ResultData getCoursePublishVoById(
            @Parameter(name = "课程ID", required = true)
            @PathVariable("id") Long id){

        CoursePublishVo coursePublishVo = courseService.getCoursePublishVo(id);
        return ResultData.success(coursePublishVo);
    }

    @Operation(summary = "根据id发布课程",description = "根据id发布课程")
    @PutMapping("/publishCourseById/{id}")
    public ResultData publishCourseById(
            @Parameter(name = "课程ID", required = true)
            @PathVariable("id") Long id){

        boolean result = courseService.publishCourseById(id);
        return ResultData.success(result);
    }

    @Operation(summary = "删除课程",description = "删除课程")
    @DeleteMapping("remove/{id}")
    public ResultData remove(@PathVariable("id") Long id) throws Exception {
        courseService.removeCourseById(id);
        return ResultData.success(null);
    }

    @GetMapping("findAll")
    public ResultData findAll() {
        List<Course> list = courseService.findlist();
        return ResultData.success(list);
    }

}

